<template>
  <div class="login">
    <div class="login-left">
      <el-image :src="loginImgUrl" fit="fill" class="login-left-img" />
    </div>
    <div class="login-right">
      <div class="login-right-from">
        <div class="login-right-blank"></div>
        <div class="login-right-main">
          <div class="login-type">
            <el-radio-group v-model="loginType">
              <el-radio-button label="登录" value="login" />
              <el-radio-button label="注册" value="register" />
            </el-radio-group>
          </div>
          <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
            <el-form-item label="用户" prop="name">
              <el-input v-model="ruleForm.name" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password" type="password" />
            </el-form-item>
            <el-form-item
              v-if="loginType === 'register'"
              label="邮箱"
              prop="email"
            >
              <el-input v-model="ruleForm.email" />
            </el-form-item>
            <el-form-item
              v-if="loginType === 'register'"
              label="手机"
              prop="phone"
            >
              <el-input v-model="ruleForm.phone" />
            </el-form-item>
            <el-form-item>
              <div class="login-from-submit">
                <el-button
                  v-if="loginType === 'login'"
                  @click="submitForm(ruleFormRef)"
                >
                  登录
                </el-button>
                <el-button
                  v-if="loginType === 'register'"
                  @click="registerForm(ruleFormRef)"
                >
                  注册
                </el-button>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from "vue";
import loginImgUrl from "@/assets/login.jpg";
import { login, register } from "@/api/login.ts";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { useRouter } from "vue-router";
interface RuleForm {
  name: string;
  password: string;
  email?: string;
  phone?: string;
}
const router = useRouter();
const loginType = ref("login");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  name: "admin",
  password: "123456",
  email: "",
  phone: "",
});
const rules = reactive<FormRules<RuleForm>>({
  name: [{ required: true, trigger: "blur" }],
  password: [{ required: true, trigger: "blur" }],
  email: [{ required: true, trigger: "blur" }],
  phone: [{ required: true, trigger: "blur" }],
});
// 登录
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (valid) {
      const { name, password } = ruleForm;
      login({ name, password }).then((res) => {
        ElMessage.success("登录成功");
        const { data } = res;
        localStorage.setItem("USER_ID", data.userId);
        router.push({ path: "/" });
      });
    }
  });
};
// 注册
const registerForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (valid) {
      const { name, password, email, phone } = ruleForm;
      register({ name, password, email, phone }).then(() => {
        ElMessage.success("注册成功");
      });
    }
  });
};
</script>
<style scoped>
.login {
  display: flex;
  height: 100vh;
}
.login-left {
  flex: 3;
}
.login-left-img {
  height: 100%;
  display: block;
}
.login-right {
  flex: 2;
}
.login-type {
  margin: 8px 0;
}
.login-right-from {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.login-right-blank {
  flex: 2;
}
.login-right-main {
  flex: 5;
  margin: 0 100px;
}
.login-from-submit {
  flex: 1;
  text-align: right;
}
</style>
